<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<title>修改个人信息</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="/webjars/miniui/boot.js" type="text/javascript"></script>
<script src="/static/js/common/common.js" type="text/javascript"></script>
<link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/static/js/aip/AIP_MAIN.js"></script>
<link href="/static/css/default.css" rel="stylesheet" type="text/css" />
<body>
<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
</div>
<div id="form1" style="margin-top: -15px;">
    <form id="user" method="post">
        <div style="width:100%;height:100%; margin: auto;padding-top: 15px;" align="center">
            <table align="center">
                <tr>
                    <td colspan="4" style="width: 100%; text-align: center;">
                        <img id="picture" src="data:image;base64,${avatar!''}"
                             style="width: 100px; height: 100px; border-radius: 5px;"
                             onclick="updateHead()" />
                    </td>
                </tr>
                <tr>
                    <td colspan="4"></td>
                </tr>
                <tr>
                    <td style="width:60px;" align="right">账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</td>
                    <td>${account!''}</td>
                    <td style="width:60px;" align="right"><font color="#dc143c">*</font>用户名:</td>
                    <td>
                        <input id="username" name="username" class="mini-textbox" width="200px" emptyText=""
                               required="true" />
                    </td>
                </tr>
                <tr>
                    <td style="width:60px;" align="right">部&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;门:</td>
                    <td>${departname!''}</td>
                    <td style="width:60px;" align="right">岗&nbsp;&nbsp;&nbsp;&nbsp;位:</td>
                    <td>${positionname!''}</td>
                </tr>
                <tr>
                    <td style="width:60px;" align="right">固&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话:</td>
                    <td>
                        <input id="telphone" name="telphone" class="mini-textbox" width="200px" emptyText="请输入固定电话"
                               required="false" onblur="checkTelPhone(telphone)" />
                    </td>
                    <td style="width:60px;" align="right">邮&nbsp;&nbsp;&nbsp;&nbsp;箱:</td>
                    <td>
                        <input id="email" name="email" class="mini-textbox" width="200px" emptyText="请输入邮箱"
                                onblur="checkEmail(email)" />
                    </td>
                </tr>
                <tr>
                    <#if sso>
                        <td style="width:60px;" align="right"><font color="#dc143c">*&nbsp;</font>手&nbsp;机&nbsp;号&nbsp;码:</td>
                        <td>
                                <input id="phone" name="phone" class="mini-textbox" width="200px" emptyText="单点登录手机号码必填"
                                       required="true" onblur="checkPhone(phone)" />
                        </td>
                    <#else >
                        <td style="width:60px;" align="right">手&nbsp;机&nbsp;号&nbsp;码:</td>
                        <td>
                            <input id="phone" name="phone" class="mini-textbox" width="200px" emptyText="请输入手机号码"
                                   required="false" onblur="checkPhone(phone)" />
                        </td>
                    </#if>

                    <td>
                        <input id="sso" name="sso" type="hidden" value="${sso}" />
                    </td>
                </tr>
            </table>
        </div>
        <div style="padding:2px;margin: 21px;text-align: center">
            <a class="mini-button" iconCls="fa-save" plain="true" onclick="SaveData()">保存</a>
            <a class="mini-button" iconCls="fa-close" plain="true" onclick="onCancel()">取消</a>
        </div>
    </form>
</div>
<script type="text/javascript">
    mini.parse();
    var account = "${account!''}";
    var phone = "${phone!''}";
    var email = "${email!''}";
    var username = "${username!''}";
    var telphone = "${telphone!''}";
    var sso = "${sso!''}";
    $("#phone").val(phone);
    $("#email").val(email);
    $("#username").val(username);
    $("#telphone").val(telphone);
    $("#sso").val(sso);
    mini.get("#phone").setValue(phone);
    mini.get("#email").setValue(email);
    mini.get("#username").setValue(username);
    mini.get("#telphone").setValue(telphone);

    function SaveData() {
        var phone = mini.get("#phone").getValue();
        var email = mini.get("#email").getValue();
        var username = mini.get("#username").getValue();
        var telphone = mini.get("#telphone").getValue();
        //单点登录省份手机号为必填
        var sso = $("#sso").val();
        var phoneFlag = false;  //手机号存在标识
        if (sso){
            if (phone == '') {
                mini.alert("单点登录省份手机号必填");
                return;
            }
            $.ajax({
                url: "/basic/user/phone",
                data:{
                    phoneNum : phone,
                    account : account
                },
                cache: false,
                async: false,
                type: 'post',
                success: function (success) {
                    if (success.code == 1) {
                        phoneFlag = true;
                    } else {
                    }
                }
            });
            if (!phoneFlag) {
                mini.alert("该手机号码已被注册，请重新输入");
                return false;
            }
        }
        if (phone == "" && telphone == "") {
            mini.alert("至少填写手机号和固话任意一项");
            return;
        }
        if (checkPhone("phone") == false) {
            return;
        }
        if (checkTelPhone("telphone") == false) {
            return;
        }
        if (checkEmail("email") == false) {
            return;
        }
        $.ajax({
            url: "/basic/user/user-info-update",
            data: {
                account: account,
                phone: phone,
                email: email,
                username: username,
                telphone: telphone
            },
            type: "post",
            success: function (data) {
                if (data.code == 1) {
                    mini.alert("修改成功！")
                } else {
                    mini.alert("修改失败！");
                }
            }
        });
    }

    function onCancel(e) {
        CloseWindow("cancel");
    }

    function updateHead() {
        var url = "/basic/user/user-head/view?account=" + account;
        var win = mini.open({
            url: url,
            allowResize: false,
            showHeader: true,
            title: '编辑头像',
            width: 250,
            height: 200
        });
        win.show();
    }

    //电话号码的验证
    function checkPhone(tel) {
        if (mini.get("phone").getValue() == '') {
            return true;
        }
        var phone = mini.get("phone").getValue();
        if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
            mini.alert('手机号码有误，请重填');
            return false;
        }
    }

    //固定电话验证
    function checkTelPhone(tel) {
        if (mini.get("telphone").getValue() == '') {
            return true;
        }
        var telphone = mini.get("telphone").getValue();

        if (!/[0]\d{2,3}[-]?\d{7,8}/.test(telphone)) {
            mini.alert('固定电话有误，请重填');
            return false;
        }
    }

    //邮箱的验证
    function checkEmail(email) {
        if (mini.get("email").getValue() == '') {
            return true;
        }
        var email = mini.get("email").getValue();
        if (!(/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/.test(email))) {
            mini.alert("邮箱有误，请重填");
            return false;
        }
    }
</script>
</body>
</html>